package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.types.TaskCategory;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TaskCategoryAdapter extends BaseAdapter {

    private Group<TaskCategory> mTaskCategories;
	private int mLayoutToInflate;
	private LayoutInflater mInflater;
	private boolean mIsLoginUser;
	private Context mContext;

	public TaskCategoryAdapter(Context context, Group<TaskCategory> taskcats, boolean isLoginUser)    {
	    mTaskCategories = taskcats;
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.task_category_item;
        mIsLoginUser = isLoginUser;
        mContext = context;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);
            holder = new ViewHolder();
            holder.icon = (ImageView)convertView.findViewById(R.id.ivTaskCategoryIcon);
            holder.number = (ImageView)convertView.findViewById(R.id.ivTaskNumber);
            holder.name = (TextView)convertView.findViewById(R.id.tvTaskCategoryName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        TaskCategory taskcat = (TaskCategory)getItem(position);
        holder.icon.setImageResource(taskcat.getIconRes());
        holder.name.setText(taskcat.getNameRes());
        holder.name.setTextAppearance(mContext, R.style.categoryText);
        Drawable localDrawable = this.mContext.getResources().getDrawable(R.drawable.category_name_background);
        holder.name.setBackgroundDrawable(localDrawable);
        holder.name.getBackground().setAlpha(150);

        int unfinish = taskcat.getTasksUndoneProcess();
        if ( mIsLoginUser ) {
            if( unfinish < 1){
                holder.number.setVisibility(View.GONE);
            } else {
                if (unfinish < 31) 
                    holder.number.setImageResource(taskcat.getNumberIconRes(unfinish-1));
                else
                    holder.number.setImageResource(taskcat.getNumberIconRes(30));
            }
        }   else {
            if (unfinish < 31) 
                holder.number.setImageResource(taskcat.getNumberDoneIconRes(unfinish-1));
            else
                holder.number.setImageResource(taskcat.getNumberDoneIconRes(30));
        }
        return convertView;
    }

	public int getCount()      {
        return mTaskCategories.size();
    }

    public Object getItem(int position)    {
        return mTaskCategories.get(position);
    }

    public long getItemId(int position)    {
        return position;
    }

	 static class ViewHolder {
	        ImageView icon;
	        ImageView number;
	        TextView name;
	 }
}

